namespace wToggle.Platform.Data
{
    using System.Runtime.Serialization;
    using System.Threading.Tasks;

    using wToggle.Platform.Interfaces;

    using XPlatUtils;

    [DataContract]
    public sealed class Project : IEntity
    {
        [DataMember(Name = "id")]
        public ulong Id { get; set; }

        [DataMember(Name = "wid")]
        public ulong WorkspaceId { get; set; }

        [DataMember(Name = "name")]
        public string Name { get; set; }

        [DataMember(Name = "billable")]
        public bool IsBillable { get; set; }

        [DataMember(Name = "is_private")]
        public bool IsPrivate { get; set; }

        [DataMember(Name = "active")]
        public bool IsActive { get; set; }

        [DataMember(Name = "color")]
        public int Color { get; set; }

        [DataMember(Name = "cid")]
        public ulong ClientId { get; set; }

        public override string ToString()
        {
            return this.Name;
        }

        [IgnoreDataMember]
        public Client Client { get; private set; }

        [IgnoreDataMember]
        public object NameWithClient
        {
            get
            {
                if (this.Client != null)
                {
                    return string.Format("{0} ({1})", this.Name, this.Client.Name);
                }

                return this.Name;
            }
        }

        public async Task LoadClient()
        {
            if (this.ClientId == 0)
            {
                return;
            }

            var provider = ServiceContainer.Resolve<IClientProvider>();

            this.Client = await provider.GetClient(this.ClientId);
        }
    }
}